package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.service.ApArticleConfigService;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.pojos.ApArticleConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Slf4j
@Transactional
public class ApArticleConfigServiceImpl extends ServiceImpl<ApArticleConfigMapper, ApArticleConfig> implements ApArticleConfigService {

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    /**
     * 文章上下架
     *
     * @param map
     */
    @Override
    public void updateByMap(Map map) {

        /**
         *  自媒体   App
         * 0 下架   1 上架
         * 0 上架   1 下架
         */
        boolean isDown = true;
        Long articleId = (Long) map.get("articleId");

        Object enable = map.get("enable");

        if (enable.equals(1)) {
            isDown = false;
        }

        //修改文章配置表
        LambdaQueryWrapper<ApArticleConfig> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(articleId != null, ApArticleConfig::getArticleId, articleId);
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(lambdaQueryWrapper);
        apArticleConfig.setIsDown(isDown);
        //更新表
        apArticleConfigMapper.updateById(apArticleConfig);

    }

    /**
     * app端文章逻辑删除
     *
     * @param articleId
     */
    @Override
    public void deleteByArticle(Long articleId) {

        //修改文章配置表

        LambdaQueryWrapper<ApArticleConfig> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(articleId != null, ApArticleConfig::getArticleId, articleId);

        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(lambdaQueryWrapper);
        apArticleConfig.setIsDelete(true);
        //更新表
        apArticleConfigMapper.updateById(apArticleConfig);

    }
}
